﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;

namespace MvcApplication2.Controllers
{
    public class AdminController : Controller
    {   //ПЫШО ЛОШОК
        private Database1Entities5 db = new Database1Entities5();//связывание контроллера с моделью
        //
        // GET: /Admin/
        //Высшие учебные заведения

        [Authorize(Roles="Administrators")]

        public ActionResult Index()//отображение всех данных
        {
            var highereducations = (from highereducation in db.Highereducations select highereducation).ToList();

            return View(highereducations);

        }

           
        [Authorize(Roles = "Administrators")]

        public ActionResult Details(int id)// детали информации о выбраном элементе
        {
            var highereducationDetails = (from highereducation in db.Highereducations
                                          where highereducation.id == id
                                          select highereducation).First();

            return View(highereducationDetails);
        }

        //
        // GET: /Admin/Create
        [Authorize(Roles = "Administrators")]
        public ActionResult Create()//отображает представление для введения новых данных

        {
            Highereducation highereducation = new Highereducation();

            return View(highereducation);
        }

       
         //
        // POST: /Admin/Create
        
        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Create(Highereducation highereducation)//обработка запроса от пользователя
        {
            try
            {
               if (ModelState.IsValid) //данные верны
                // TODO: Add insert logic here
               {
                   db.AddToHighereducations(highereducation);//добавление нового
                   db.SaveChanges();//сохранение изменений
                   return RedirectToAction("Index");}//
            }
            catch(Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex);//исключение
            }
            return View(highereducation);
        }
      

                
        //
        // GET: /Admin/Edit/5
 [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id)
        {
            var highereducationEdit = (from highereducation in db.Highereducations
                                          where highereducation.id == id
                                          select highereducation).First();

            return View(highereducationEdit);
        }

        //
        // POST: /Admin/Edit/5
        
        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var highereducationEdit = (from highereducation in db.Highereducations
                                       where highereducation.id == id
                                       select highereducation).First();
            try
            {
                UpdateModel(highereducationEdit);//сохранение изменений
                db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View(highereducationEdit);
            }
        }

        //
        // GET: /Admin/Delete/5
 [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id)
        {
            var highereducationDelete = (from highereducation in db.Highereducations
                                       where highereducation.id == id
                                       select highereducation).First();

            return View(highereducationDelete);
          
        }

        //
        // POST: /Admin/Delete/5

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            var highereducationDelete = (from highereducation in db.Highereducations
                                         where highereducation.id == id
                                         select highereducation).First();
            try
            {
                db.DeleteObject(highereducationDelete);//удаление
                db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View(highereducationDelete);
            }

        }



        //Факультеты

        [Authorize(Roles = "Administrators")]

        public ActionResult Facult(int id)//отображение всех данных
        {
            var facultys = (from faculty in db.Facultys
                            where faculty.idhe == id
                            select faculty).ToList();
            return View(facultys);

        }

        [Authorize(Roles = "Administrators")]

        public ActionResult DetailsFacult(int id)// детали информации о выбраном элементе
        {
            var facultysdetails = (from faculty in db.Facultys
                                      where faculty.id == id
                                      select faculty).First();

            return View(facultysdetails);
        }
        // GET: /Admin/Create
        //[Authorize(Roles = "Administrators")]
        //public ActionResult Createfaculty(int m)//отображает представление для введения новых данных
        //{
        //   Faculty faculty = new Faculty();
        //   faculty.idhe = m;
        //    return View(faculty);
        //}


        //
        // POST: /Admin/Create

        //[HttpPost]
        //[Authorize(Roles = "Administrators")]
        //public ActionResult Createfaculty(Faculty faculty)//обработка запроса от пользователя
        //{
        //    try
        //    {
        //        if (ModelState.IsValid) //данные верны
        //        // TODO: Add insert logic here
        //        {
        //            db.AddToFacultys(faculty);//добавление нового
        //            db.SaveChanges();//сохранение изменений
        //            return RedirectToAction("Faculty");
        //        }//
        //    }
        //    catch (Exception ex)
        //    {
        //        ModelState.AddModelError(String.Empty, ex);//исключение
        //    }
        //    return View(faculty);
        //}

        //
        // GET: /Admin/Edit/5
        [Authorize(Roles = "Administrators")]
        public ActionResult Editfaculty(int id)
        {
            var facultyEdit = (from faculty in db.Facultys
                               where faculty.id == id
                               select faculty).First();

            return View(facultyEdit);
        }

        //
        // POST: /Admin/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Editfaculty(int id, FormCollection collection)
        {
            var facultyEdit = (from faculty in db.Facultys
                               where faculty.id == id
                               select faculty).First();
            try
            {
                UpdateModel(facultyEdit);//сохранение изменений
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(facultyEdit);
            }
        }

       // //
       // // GET: /Admin/Delete/5
       // [Authorize(Roles = "Administrators")]
       // public ActionResult Deletefaculty(int id)
       // {
       //     var facultyDelete = (from faculty in db.Facultys
       //                          where faculty.id == id
       //                          select faculty).First();

       //     return View(facultyDelete);

       // }

       //// //
       //// // POST: /Admin/Delete/5

       // [HttpPost]
       // [Authorize(Roles = "Administrators")]
       // public ActionResult Deletefaculty(int id, FormCollection collection)
       // {
       //     var facultyDelete = (from faculty in db.Facultys
       //                          where faculty.id == id
       //                          select faculty).First();

       //     try
       //     {
       //         db.DeleteObject(facultyDelete);//удаление
       //         db.SaveChanges();

       //         return RedirectToAction("Index");
       //     }
       //     catch
       //     {
       //         return View(facultyDelete);
       //     }
       // }

       // //Специальности

        [Authorize(Roles = "Administrators")]

        public ActionResult Speciality(int id)
        {
            var specialitys = (from speciality in db.Specialitys
                               where speciality.idfac == id
                               select speciality).ToList();
            return View(specialitys);
        }


        [Authorize(Roles = "Administrators")]

        public ActionResult DetailsSpeciality(int id)// детали информации о выбраном элементе
        {
            var specialitysdetails = (from speciality in db.Specialitys
                                      where speciality.number == id
                                      select speciality).First();

            return View(specialitysdetails);
        }

        //[Authorize(Roles = "Administrators")]
        public ActionResult Editspeciality(int id)
        {
            var specialityEdit = (from speciality in db.Specialitys
                                  where speciality.number == id
                                  select speciality).First();

            return View(specialityEdit);
        }

        ////
        //// POST: /Admin/Edit/5

        //[HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Editspeciality(int id, FormCollection collection)
        {
            var specialityEdit = (from speciality in db.Specialitys
                                  where speciality.number == id
                                  select speciality).First();
            try
            {
                UpdateModel(specialityEdit);//сохранение изменений
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(specialityEdit);
            }
        }

        
            
        
       // // GET: /Admin/Delete/5
        //[Authorize(Roles = "Administrators")]
        //public ActionResult Delete2(int id)
        //{
        //    var specialitydelete = (from speciality in db.Specialitys
        //                            where speciality.number == id
        //                            select speciality).First();

        //    return View(specialitydelete);

        //}

        
        
       ////  POST: /Admin/Delete/5

        //[HttpPost]
        //[Authorize(Roles = "Administrators")]
        //public ActionResult Delete2(int id, FormCollection collection)
        //{
        //    var specialitydelete = (from speciality in db.Specialitys
        //                            where speciality.number == id
        //                            select speciality).First();


        //    try
        //    {
        //        db.DeleteObject(specialitydelete);//удаление
        //        db.SaveChanges();

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View(specialitydelete);
        //    }
        //}

       //Статистика по годам

        public ActionResult Score(int number)
        {
            var scores = (from score in db.Scores
                          where score.id_spec == number
                          select score).ToList();

            return View(scores);
        }

        [Authorize(Roles = "Administrators")]

        public ActionResult DetailsScore(int id)// детали информации о выбраном элементе
        {
            var scoreDetails = (from score in db.Scores
                                          where score.id == id
                                          select score).First();

            return View(scoreDetails);
                    }

        [Authorize(Roles = "Administrators")]
        public ActionResult EditScore(int id)
        {
            var scoreEdit = (from score in db.Scores
                             where score.id == id
                             select score).First();

            return View(scoreEdit);
        }

        //
        // POST: /Admin/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult EditScore(int id, FormCollection collection)
        {
            var scoreEdit = (from score in db.Scores
                                       where score.id == id
                                       select score).First();
            try
            {
                UpdateModel(scoreEdit);//сохранение изменений
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(scoreEdit);
            }
        }

        // GET: /Admin/Delete/5
        //[Authorize(Roles = "Administrators")]
        //public ActionResult DeleteScore(int id)
        //{
        //    var scoreDelete = (from score in db.Scores
        //                                 where score.id == id
        //                                 select score).First();

        //    return View(scoreDelete);

        //}

        //
        // POST: /Admin/Delete/5

        //[HttpPost]
        //[Authorize(Roles = "Administrators")]
        //public ActionResult DeleteScore(int id, FormCollection collection)
        //{
        //    var scoreDelete = (from score in db.Scores
        //                       where score.id == id
        //                       select score).First();
        //    try
        //    {
        //        db.DeleteObject(scoreDelete);//удаление
        //        db.SaveChanges();

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View(scoreDelete);
        //    }
        //}

        //Предметы необходимые для сдачи
        public ActionResult specSubDetails(int number)
        {
            List<MvcApplication2.Models.Subject> subjectsToView = new List<MvcApplication2.Models.Subject>();

            var specSubs = (from specSub in db.SpecSubs
                            where specSub.idspec == number
                            select specSub).ToList();


            foreach (SpecSub ss in specSubs)
            {
                var singleSubject = (from subject in db.Subjects
                                     where subject.id == ss.idsub
                                     select subject).SingleOrDefault();

                subjectsToView.Add(singleSubject);
            }

            return View(subjectsToView);
        }

        [Authorize(Roles = "Administrators")]

        public ActionResult DetailsspecSub(int id)// детали информации о выбраном элементе
        {
            var specSubDetails = (from subject in db.Subjects
                                where subject.id == id
                                select subject).First();

            return View(specSubDetails);
        }
        
        [Authorize(Roles = "Administrators")]
        public ActionResult EditspecSub(int id)
        {
            var specSubEdit = (from subject in db.Subjects
                             where subject.id == id
                             select subject).First();

            return View(specSubEdit);
        }

        //
        // POST: /Admin/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult EditspecSub(int id, FormCollection collection)
        {
            var specSubEdit = (from subject in db.Subjects
                             where subject.id == id
                             select subject).First();
            try
            {
                UpdateModel(specSubEdit);//сохранение изменений
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(specSubEdit);
            }
        }

        // GET: /Admin/Delete/5
        //[Authorize(Roles = "Administrators")]
        //public ActionResult DeletespecSub(int id)
        //{
        //    var specSubDelete = (from specSub in db.SpecSubs
        //                         where specSub.idsub == id
        //                         select specSub).First();

        //    return View(specSubDelete);

        //}

        //
        // POST: /Admin/Delete/5

        //[HttpPost]
        //[Authorize(Roles = "Administrators")]
        //public ActionResult DeletespecSub(int id, FormCollection collection)
        //{
        //    var specSubDelete = (from specSub in db.SpecSubs
        //                         where specSub.idsub == id
        //                         select specSub).First();
        //    try
        //    {
        //        db.DeleteObject(specSubDelete);//удаление
        //        db.SaveChanges();

        //        return RedirectToAction("Index");
        //    }
        //    catch
        //    {
        //        return View(specSubDelete);
        //    }
        //}




        public ActionResult SA(int id)
        {
            

            var anspecs = (from anketa in db.Anketas
                           where anketa.id_special == id
                           select anketa).ToList();



            return View(anspecs);
        }
        [Authorize(Roles = "Administrators")]
        public ActionResult EditAnketa(int id)
        {
            var anketaEdit = (from anketa in db.Anketas
                               where anketa.id == id
                               select anketa).First();

            return View(anketaEdit);
        }

        //
        // POST: /Admin/Edit/5

        [HttpPost]
        [Authorize(Roles = "Administrators")]
        public ActionResult Editanketa(int id, FormCollection collection)
        {
            var anketaEdit = (from anketa in db.Anketas
                              where anketa.id == id
                              select anketa).First();
            try
            {
                UpdateModel(anketaEdit);//сохранение изменений
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View(anketaEdit);
            }
        }
    }
}
